package com.baijia.tianxiao.connect.config;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.baijia.tianxiao.connect.common.constant.Constant;

@Component
public class RegisterAddrConfig {
	
	private static Logger logger = LoggerFactory.getLogger(RegisterAddrConfig.class);
	@Autowired
	private StringRedisTemplate businessRedisTemplate;

	public void registerAddr(String ip, Integer port) {
		try{
			// 等第一个请求建立时，初始化端口信息
			String addr = ip+":"+port;
			logger.info("register ip:host ["+addr+"]");
			List<String> existHosts = businessRedisTemplate.opsForList().range(Constant.CC_SERVER_URL_KEY, 0, businessRedisTemplate.opsForList().size(Constant.CC_SERVER_URL_KEY)-1);
			if(CollectionUtils.isNotEmpty(existHosts) && existHosts.contains(addr)){
				
			}else{
				businessRedisTemplate.opsForList().leftPush(Constant.CC_SERVER_URL_KEY, addr);
			}
		}catch (Exception e) {
			logger.error("Register addr to redis error! This will cause vzhibo to send a message fail!", e);
		}
	}

}
